package custom.base.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import static android.content.Context.CLIPBOARD_SERVICE;

/**
 * Created by cubebox on 2017/12/7.
 */

public class ClipboardUtils {
    /**
     * 复制到剪切板
     */
    public static void clipToBorad(Context context, String content) {
        if (android.os.Build.VERSION.SDK_INT > 11) {
            ClipboardManager c = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
            c.setPrimaryClip(ClipData.newPlainText(AppUtils.getAppName(context), content));
        } else {
            android.text.ClipboardManager c = (android.text.ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
            c.setText(content);
        }
    }

    /**
     * 获取最新剪切板的信息
     */
    public static String getClipBoradContent(Context context) {
        ClipboardManager cm = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
        ClipData data = cm.getPrimaryClip();
        String content = "";
        if (data != null && data.getItemCount() > 0) {
            ClipData.Item item = data.getItemAt(0);//获取第0条消息
            content = item.getText().toString();
        }
        return content;
    }

}
